import pymysql

def select_db(select_sql):
    """查询"""
    # 建立数据库连接
    db = pymysql.connect(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="cs"
    )
    # 通过 cursor() 创建游标对象，并让查询结果以字典格式输出
    cur = db.cursor(cursor=pymysql.cursors.DictCursor)
    # 使用 execute() 执行sql
    cur.execute(select_sql)
    # 使用 fetchall() 获取所有查询结果
    data = cur.fetchall()
    # 关闭游标
    db.commit()
    cur.close()
    # 关闭数据库连接
    db.close()
    return data


if __name__ == '__main__':
    insert_sql1 = 'INSERT INTO user(name) VALUES("王4354345")'
    select_sql = 'SELECT * FROM user'
    update_sql2 = 'UPDATE user SET name = "张打完大无" WHERE id = 1'
    delete_sql = 'DELETE FROM user WHERE id = 1'
    print(select_db(delete_sql))
    # for i in select_db(select_sql):
    #     print(i)
